package ds.tree;

import java.util.HashMap;

public class VerticalSumOfTree {

	private HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>();
	
	private static class TreeNode{
		int value;
		TreeNode left = null;
		TreeNode right = null;
		
		TreeNode(int val){
			this.value = val;
		}
		
		public String toString(){
			return String.valueOf(value);
		}
	}

	public void verticalSum(TreeNode node, int hd){
		if(node == null){
			return;
		}
		verticalSum(node.left , hd -1);
		int prevSum = (hm.get(Integer.valueOf(hd)) == null)? 0 : hm.get(Integer.valueOf(hd));
		hm.put(hd, (prevSum+node.value));
		verticalSum(node.right, hd+1);
	}
	private static TreeNode tree(){
		TreeNode A = new TreeNode(1);
		TreeNode B = new TreeNode(2);
		TreeNode C = new TreeNode(3);
		TreeNode D = new TreeNode(4);
		TreeNode E = new TreeNode(5);
		TreeNode F = new TreeNode(6);
		TreeNode G = new TreeNode(7);
		
		A.left = B;
		A.right = C;
		B.left = D;
		B.right = E;
		C.left = F;
		C.right = G;
		
		return A;
	}

	public static void main(String[] args) {
		VerticalSumOfTree verticalSum = new VerticalSumOfTree();
		verticalSum.verticalSum(tree(), 0);
		System.out.println(verticalSum.hm.entrySet());
	}

}
